package com.wuzhishan.news.controller;

import com.wuzhishan.news.pojo.BlogUser;
import com.wuzhishan.news.pojo.Result;
import com.wuzhishan.news.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class PublicController {
    @Autowired
    private userService _userService;


    @RequestMapping("/register.html")
    public Object go_register(ModelAndView mv){
        mv.setViewName("register");
        return mv;
    }

    @RequestMapping("/public/register")
    @ResponseBody
    public Object register(BlogUser formUser){
        Map<String,Object> result = _userService.register(formUser);
        return result;
    }

    @RequestMapping("/public/login")
    @ResponseBody
    public Object login(String username, String password, HttpSession session){
        Result result = _userService.login(username,password,session);
        return result;
    }

    @RequestMapping("/public/logout")
    public Object login(HttpSession session){
        session.removeAttribute("loginInfo");
        return "redirect:/login.html";
    }

    @RequestMapping("/admin")
    public Object login(ModelAndView mv){
        mv.setViewName("/admin/admin");
        return mv;
    }

}
